<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('下级单位用户')" />
</head>
<body class="gray-bg">
     <div class="container-div" id="xiajidanweiyonghu_list">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <label style="width: 80px !important;">特贴单位&nbsp;&nbsp;</label>
                                <input style="width: 150px !important;" type="text" name="deptname" v-model="deptname"/>
                            </li>
                            <li>
                                <label style="width: 80px !important;">单位类型&nbsp;&nbsp;</label>
                                <select style="width: 150px !important;" name="depttype" th:with="type=${@dict.getType('sys_jigouleixing')}" v-model="depttype" class="form-control" type="text">
                                    <option value="">请选择</option>
                                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                                </select>
                            </li>
                            <li>
                                <label style="width: 80px !important;">省份&nbsp;&nbsp;</label>
                                <select style="display: inline;width: 150px !important;" id="shengfen" name="shengfen" v-model="shengfen" class="form-control" type="text" @change="BindChengshiByShengfen()">
                                    <option value="">请选择</option>
                                    <option v-for="shengfen in shengfenlist" :value="shengfen.id">{{shengfen.text}}</option>
                                </select>
                            </li>
                            <li>
                                <label style="width: 80px !important;">城市&nbsp;&nbsp;</label>
                                <select style="display: inline;width: 150px !important;" id="chengshi" name="chengshi" v-model="chengshi" class="form-control" type="text">
                                    <option value="">请选择</option>
                                    <option v-for="chengshi in chengshilist" :value="chengshi.id">{{chengshi.text}}</option>
                                </select>
                            </li>
                            <li class="sousuo">
                                <a class="btn btn-success btn-md" @click="bindDeptList"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-md" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="col-sm-12 select-table table-striped">

                <div style="display: flex">
                    <div class="btn-group-md" id="toolbar" role="group">
                        <button class="btn btn-warning dim" @click="Export">
                            <i class="fa fa-download"></i> 导出
                        </button>
                    </div>
                </div>

                <table id="bootstrap-table" class="table table-bordered table-hover table-striped">
                    <thead>
                        <tr>
                            <th style="width: 50px;">序号</th>
                            <th>特贴单位</th>
                            <th>单位类型</th>
                            <th>地区</th>
                            <th>负责人</th>
                            <th>负责人办公电话</th>
                            <th>单位状态</th>
                            <th style="width: 300px;">操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr v-for="(deptitem,index) in deptlist"  v-if="deptlist.length > 0">
                            <td v-text="index+1"></td>
                            <!--单位名称-->
                            <td v-text="deptitem.deptname" style=" text-align: center;color:#1c84c6;cursor: pointer" title="联系方式" @click="toLianxifangshi(deptitem.deptid)"></td>
                            <!--单位类型-->
                            <td v-text="deptitem.depttype_text"></td>
                            <!--地区-->
                            <td style="vertical-align: middle; text-align: center;" v-if="deptitem.chengshi_text!=null" v-text="deptitem.shengfen_text+deptitem.chengshi_text"></td>
                            <td style="vertical-align: middle; text-align: center;" v-else v-text="deptitem.shengfen_text"></td>
                            <!--负责人-->
                            <td v-text="deptitem.fuzeren"></td>
                            <!--负责人办公电话-->
                            <td v-text="deptitem.fuzerenbangongdianhua"></td>
                            <!--单位状态-->
                            <td v-if="deptitem.status=='1'"><span class="badge badge-primary">正常</span></td>
                            <td v-else><span class="badge badge-danger">停用</span></td>
                            <!--操作-->
                            <td>
                                <button class="btn btn-success btn-md" @click="showUser(deptitem.deptid)">人员管理</button>
                            </td>
                        </tr>
                        <tr v-if="deptlist.length == 0">
                            <td style="text-align: center; vertical-align: middle" colspan="8">查无数据</td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>

         <!-- 查看单位联系方式 -->
         <div th:replace="system/danweiguanli/modal_lianxifangshi::lianxifangshi"></div>
         <!-- 管理单位人员 -->
         <div th:replace="system/yonghuguanli/modal_user::user_manage"></div>

    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">

        var prefix = ctx + "system/deptxinxi";
        var prefix_user = ctx + "system/user";

        var vm_xiajidanweiyonghu_list = new Vue({
            el: "#xiajidanweiyonghu_list",
            data: {
                deptid:$("#deptid").val(),//当前登录的单位ID
                jigoujibie: $("#jigoujibie").val(),
                roleid: $("#roleid").val(),

                //查询用字段
                deptname: "",//单位名称
                depttype: "",//单位类型
                shengfen: "",//省份
                chengshi: "",//城市
                status: "",//单位状态

                //字典表
                shengfenlist:[],//省份
                chengshilist:[],//城市

                //查询列表
                deptlist:[],//单位列表
                dept:{},//单个单位

                //用户管理
                islistshow:true,//列表
                isuseraddshow:false,//添加
                isusereditshow:false,//修改

                shifouyiyouchaoguan:'',//是否已有超管

                userlist:[],//用户列表
                user:{},//用户
                userdeptid:'',//用户单位ID


                //edit字段
                editItem:{
                    loginname: "",
                    phonenumber: "",
                    email: "",
                    password:"",
                    username:"",
                    suoshubumen:"",//所属部门
                    bangongdianhua:"",//办公电话
                    remark:"",
                    roleid:""
                },
            },
            created: function () {
                var _this = this;

                //初始化界面控制
                _this.InitControl();
                //绑定单位列表
                _this.bindDeptList();

            },
            watch:{
            },
            methods: {

                InitControl:function(){
                    var _this = this;

                    _this.BindShengfen();
                    _this.BindDiqu();
                    _this.BindChengshiByShengfen();
                },

                BindShengfen:function(){
                    var _this = this;

                    var dept_layer = layer.open({type: 3});
                    $.post('/system/shengfen/findallshengfen', {
                        rdm: Math.random()
                    }, function (ppData) {
                        layer.close(dept_layer);

                        if (ppData != null) {
                            var data = ppData;
                            _this.shengfenlist = data.resultContent;
                        }
                    }, "json");
                },

                BindDiqu:function(){
                    var _this = this;

                    var dept_layer = layer.open({type: 3});
                    $.post('/system/chengshi/findallchengshi', {
                        rdm: Math.random()
                    }, function (ppData) {
                        layer.close(dept_layer);

                        if (ppData != null) {
                            var data = ppData;
                            _this.chengshilist = data.resultContent;
                        }
                    }, "json");
                },

                BindChengshiByShengfen:function(){
                    var _this = this;

                    _this.dept.chengshi = '';

                    var dept_layer = layer.open({type: 3});
                    $.post('/system/chengshi/findallchengshi', {
                        shengfenid : _this.shengfen,
                        rdm: Math.random()
                    }, function (ppData) {
                        layer.close(dept_layer);

                        if (ppData != null) {
                            var data = ppData;
                            _this.chengshilist = data.resultContent;
                        }
                    }, "json");
                },

                // 绑定单位列表
                bindDeptList: function () {
                    var _this = this;

                    var dept_layer = layer.open({type: 3});
                    $.post(prefix+'/customlist', {
                        parentid: _this.deptid,
                        deptname: _this.deptname,
                        depttype: _this.depttype,
                        shengfen: _this.shengfen,
                        chengshi:_this.chengshi,
                        status:_this.status,
                        rdm: Math.random()
                    }, function (ppData) {
                        layer.close(dept_layer);

                        if (ppData != null) {
                            if (ppData.result=='1'){
                                var data = ppData.resultContent;
                                _this.deptlist = data;
                            }else{
                                layer.alert(ppData.message);
                            }
                        }
                    }, "json");
                },

                // 点击单位名称，联系方式
                toLianxifangshi:function (deptid) {
                    var _this = this;

                    $("#myModalLabel2").html("单位联系方式");

                    _this.deptid=deptid
                    _this.dept = {};
                    _this.bindDept();
                    $("#lianxifangshiModal").modal();
                },

                // 查询单个单位
                bindDept: function () {
                    var _this = this;

                    var dept_layer = layer.open({type: 3});
                    $.post(prefix+'/customone', {
                        deptid: _this.deptid,
                        rdm: Math.random()
                    }, function (ppData) {
                        layer.close(dept_layer);

                        if (ppData != null) {
                            if (ppData.result=="1") {
                                var data = ppData.resultContent;
                                _this.dept = data;
                                _this.dept.deptid = _this.deptid;
                            } else {
                                layer.alert(ppData.message);
                            }
                        }
                    }, "json");
                },

                //-----------------------------------------用户管理------------------------------------------------------

                //显示用户管理
                showUser: function (ppDeptId) {
                    var _this = this;

                    _this.bindUserList(ppDeptId)
                    $("#userModal").modal();

                    _this.userdeptid = ppDeptId;
                    $('#userModal').on('hidden.bs.modal', function () {
                        _this.islistshow = true;
                        _this.isuseraddshow = false;
                        _this.isusereditshow = false;
                    })
                },

                //绑定用户列表
                bindUserList: function (ppDeptId) {
                    var _this = this
                    $.post(prefix_user+'/finduserbydeptid', {
                        deptid: ppDeptId,
                    }, function (ppData) {
                        if (ppData != null) {
                            var data = ppData;
                            _this.userlist = data.resultContent;

                            for (var i=0;i<data.length;i++){
                                if(data[i].role_id=='102'){
                                    _this.shifouyiyouchaoguan = 1;
                                    break;
                                }
                            }

                        } else {
                            layer.alert(ppData.message);
                        }
                    }, "json");
                },

                //点击新增
                userAdd: function () {
                    var _this = this;
                    _this.islistshow = false;
                    _this.isuseraddshow = true;

                    _this.editItem = {
                        loginname: "",
                        phonenumber: "",
                        email: "",
                        password:"",
                        username:"",
                        suoshubumen:"",//所属部门
                        bangongdianhua:"",//办公电话
                        remark:"",
                        roleid:""
                    }

                },

                //返回列表
                closeUserEdit: function () {
                    var _this = this;
                    _this.bindUserList(_this.userdeptid);
                    _this.islistshow = true;
                    _this.isuseraddshow = false;
                    _this.isusereditshow = false;
                },

                submitHandler:function () {
                    var _this = this;
                    var prefix = "/system/user";
                    $("#form-user-add").validate({
                        onkeyup: false,
                        rules: {
                            loginName: {
                                minlength: 2,
                                maxlength: 20,
                                remote: {
                                    url:  prefix + "/checkLoginNameUnique",
                                    type: "post",
                                    dataType: "json",
                                    data: {
                                        "loginName": function () {
                                            return $.common.trim(_this.editItem.loginname);
                                        }
                                    },
                                    dataFilter: function (data, type) {
                                        return $.validate.unique(data);
                                    }
                                }
                            },
                            password: {
                                required:true,
                                minlength: 8,
                                maxlength: 20
                            },
                        },
                        messages: {
                            "loginName": {
                                remote: "用户已经存在"
                            },
                        },
                        focusCleanup: true
                    });
                    if ($.validate.form("form-user-add") && checkpwd(0, _this.editItem.password)) {
                        $.post('/system/user/customadd', {
                            deptId: _this.userdeptid,
                            loginName:_this.editItem.loginname,
                            phonenumber:_this.editItem.phonenumber,
                            email:_this.editItem.email,
                            password:_this.editItem.password,
                            userName:_this.editItem.username,
                            roleIds:_this.editItem.roleid,
                            suoshubumen:_this.editItem.suoshubumen,
                            bangongdianhua:_this.editItem.bangongdianhua,
                            remark:_this.editItem.remark,
                            rdm: Math.random()
                        }, function (ppData) {
                            if(ppData.code==0){
                                setTimeout(function(){
                                    _this.islistshow = true
                                    _this.isuseraddshow = false
                                }, 10,);

                                _this.editItem = {
                                    loginname: "",
                                    phonenumber: "",
                                    email: "",
                                    password:"",
                                    username:"",
                                    suoshubumen:"",//所属部门
                                    bangongdianhua:"",//办公电话
                                    remark:"",
                                    roleid:""
                                }

                                layer.open({
                                    time: 1000,
                                    btn: [],
                                    content: "新增成功!",
                                });
                                _this.bindUserList(_this.userdeptid);
                            }else {
                                layer.alert(ppData.msg)
                            }
                        }, "json");
                    }
                },

                //点击修改
                userModify(user){
                    var _this = this;
                    _this.user = user;
                    _this.islistshow=false;
                    _this.isusereditshow=true;

                    _this.editItem = {
                        phonenumber: user.phonenumber,
                        email: user.email,
                        sex:user.sex,
                        username:user.user_name,
                        loginname: user.login_name,
                        userid:user.user_id,
                        suoshubumen:user.text1,//所属部门
                        bangongdianhua:user.text2,//办公电话
                        remark:user.remark,
                        roleid:user.role_id
                    }
                },

                //返回列表
                closeuserModify(){
                    var _this = this;
                    _this.bindUserList(_this.userdeptid);
                    _this.islistshow = true;
                    _this.isusereditshow = false;
                },

                submitUserEdit:function () {
                    var _this = this;
                    $("#form-user-edit").validate({
                        onkeyup: false,
                        rules:{
                            userName:{
                                required:true
                            },
                            password: {
                                minlength: 8,
                                maxlength: 20
                            },
                        },
                        messages: {
                        },
                        focusCleanup: true
                    });
                    if ($.validate.form("form-user-edit") && checkpwd(0, _this.editItem.password)) {
                        $.post('/system/user/customedit', {
                            userId:_this.editItem.userid,
                            phonenumber:_this.editItem.phonenumber,
                            email:_this.editItem.email,
                            userName:_this.editItem.username,
                            loginName:_this.editItem.loginname,
                            suoshubumen:_this.editItem.suoshubumen,//所属部门
                            bangongdianhua:_this.editItem.bangongdianhua,//办公电话
                            remark:_this.editItem.remark,
                            rdm: Math.random()
                        }, function (ppData) {
                            if(ppData.code==0){
                                setTimeout(function(){
                                    _this.islistshow = true
                                    _this.isusereditshow = false
                                }, 10,)
                                layer.open({
                                    time: 1000,
                                    btn: [],
                                    content: "修改成功!",
                                });
                                _this.bindUserList(_this.userdeptid)
                            }else {
                                layer.alert(ppData.msg)
                            }
                        }, "json");
                    }
                },

                //删除用户
                userRemove(userId){
                    var _this = this;
                    $.modal.confirm("确认删除", function() {
                        $.post(prefix_user+'/customdelete', {
                            userid: userId,
                        }, function (ppData) {
                            if (ppData != null) {
                                var data = ppData;
                                if (data.code==0){
                                    layer.open({
                                        time: 1000,
                                        btn: [],
                                        content: "删除成功!",
                                    });

                                    _this.bindUserList(_this.userdeptid);
                                }
                            } else {
                                layer.alert(ppData.message);
                            }
                        }, "json");
                    });
                },

                Export:function(){
                    var _this = this
                    location.href = "/system/daochu/deptxinxi?parentid="+_this.deptid
                        +"&deptname="+_this.deptname
                        +"&depttype="+_this.depttype
                        +"&shengfen="+_this.shengfen
                        +"&chengshi="+_this.chengshi
                },

                resetPwd:function (user) {
                    var url = '/system/yonghuguanli/resetPwd/' + user.user_id;
                    $.modal.open("重置密码", url, '800', '300');
                },
            }
        })
    </script>
</body>
</html>